package com.maishi.programmer.jainzhioffer;

import java.util.ArrayList;

/**
 * Created by brossdong on 2018/4/2.
 * 从上往下打印出二叉树的每个节点，同层节点从左至右打印。
 */
public class TestProblem32 {

    /**
     * 构造树
     */
    public static class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;
        public TreeNode (int val) {
            this.val = val;
        }
    }


    public static ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
        ArrayList<TreeNode> arrayList = new ArrayList<>();
        ArrayList<Integer> list = new ArrayList<>();
        if (root == null) return list;
        arrayList.add(root);
        while (arrayList.size() > 0) {
            TreeNode temp = arrayList.remove(0);
            if (temp.left != null) {
                arrayList.add(temp.left);
            }
            if (temp.right != null) {
                arrayList.add(temp.right);
            }
            list.add(temp.val);
        }
        return list;
    }


    public static void main(String[] args) {
        ArrayList<TreeNode> list = new ArrayList<>();
        list.add(0,new TreeNode(8));
        list.add(1,new TreeNode(6));
        list.add(2,new TreeNode(10));

        ArrayList<Integer> arrayList = PrintFromTopToBottom(list.get(0));
        System.out.println(arrayList);

    }

}
